home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 …ember: Reference Library / Dev.CD Dec 00 RL Disk 1.toast / pc / technical documentation / develop / develop issue 27 / develop issue 27 code / 3d game controls / source / file.c < prev    next >
Encoding:
Text File  |  1996-06-29  |  5.3 KB  |  216 lines

  1. //--------------------------------------------------------------------------------------------
  2. //  File Handling Code
  3. //
  4. //      by Philip McBride
  5. //        parts taken from Apple DTS sample code
  6. //
  7. //--------------------------------------------------------------------------------------------
  8.  
  9.  
  10. #include "GameControls.h"
  11. #include "extern.h"
  12. #include "draw.h"
  13. #include "file.h"
  14. #include "lights.h"
  15. #include "camera.h"
  16.  
  17.  
  18. //--------------------------------------------------------------------------------------------
  19. //  Open a File for Reading
  20. //
  21. OSErr MyOpenFile(FSSpec *theFile, DocumentPtr theDocument)
  22. {    OSErr                result;
  23.     short                refNum;
  24.     FInfo                fndrInfo ;
  25.     
  26.     FSpGetFInfo( theFile, &fndrInfo ) ;
  27.     
  28.     // Open file
  29.     if ((result = FSpOpenDF(theFile, fsRdWrPerm, &refNum)) != noErr)
  30.         return(result);
  31.     
  32.     // Assign file info to document
  33.     theDocument->fRefNum = refNum;
  34.     theDocument->theFileSpec = *theFile;
  35.     
  36.     // Set cursor and read model file
  37.     SetCursor(*GetCursor(watchCursor));
  38.     result = MyReadDocumentFile( theDocument);
  39.     
  40.     // Show window w/ title and reset cursor
  41.     SetWTitle((WindowPtr)theDocument->theWindow, theFile->name);
  42.     ShowWindow((WindowPtr)theDocument->theWindow);
  43.     SetCursor(&qd.arrow);
  44.     
  45.     return(result);
  46. }
  47.  
  48. //--------------------------------------------------------------------------------------------
  49. //  Setup 3D Storage for Reading
  50. //
  51. OSErr MyReadDocumentFile(DocumentPtr theDocument)
  52. {
  53.     TQ3StorageObject    storage;
  54.     TQ3FileObject        fd;
  55.     TQ3Object             objects = nil;
  56.     TQ3Status            status;
  57.     
  58.     // create new storage and file objects
  59.     storage = Q3MacintoshStorage_New( theDocument->fRefNum );
  60.     if (storage == nil)
  61.         goto bail;
  62.     fd = Q3File_New();
  63.     if (fd == nil)
  64.         goto bail;
  65.  
  66.     // associate the storage with the file
  67.     Q3File_SetStorage(fd, storage);
  68.     Q3Object_Dispose(storage);
  69.     
  70.     // read the drawable objects from the file object into a new group
  71.     status = MyReadScene(fd, theDocument);
  72.     if (status == kQ3Failure)
  73.         goto bail;
  74.     
  75.     Q3Object_Dispose(fd);
  76.  
  77.     return(noErr);
  78.     
  79. bail:
  80.     Q3Object_Dispose(fd);
  81.  
  82.     return(fnOpnErr);
  83. }
  84.  
  85. //--------------------------------------------------------------------------------------------
  86. //  Read the 3D scene into Memory (create lights and camera if not included)
  87. //
  88. TQ3Status MyReadScene(TQ3FileObject file, DocumentPtr theDocument)
  89. {
  90.     TQ3Object            object;
  91.     TQ3Boolean            isEOF;
  92.     TQ3ViewObject        view;
  93.     TQ3Object             model = NULL;
  94.     TQ3GroupObject         lightGroup = NULL;
  95.     TQ3Object            viewHints = NULL;
  96.     TQ3CameraObject        camera = NULL;
  97.     TQ3AttributeSet        attributeSet = NULL;
  98.     TQ3RendererObject    renderer = NULL;
  99.     
  100.     // Create new model and get view                
  101.     model = Q3DisplayGroup_New();
  102.     if (!model)
  103.         return kQ3Failure;
  104.     theDocument->documentGroup = model;
  105.     view = theDocument->theView;
  106.     
  107.     SetCursor(*GetCursor(watchCursor));
  108.  
  109.     // Open file for reading
  110.     if (Q3File_OpenRead(file, NULL) != kQ3Success) {
  111.         SetCursor(&qd.arrow);
  112.         return kQ3Failure;
  113.     }
  114.  
  115.     // Collect all drawable objects (into model) and any viewhints
  116.     while ((isEOF = Q3File_IsEndOfFile(file)) == kQ3False) {
  117.         object = Q3File_ReadObject(file);
  118.         
  119.         if (Q3Object_IsDrawable(object)) {
  120.         
  121.             Q3Group_AddObject(model, object);
  122.             
  123.         } else if (Q3Object_IsType(object, kQ3SharedTypeViewHints)) {
  124.         
  125.             if (viewHints == NULL)
  126.             {
  127.                 viewHints = object;
  128.                 theDocument->viewHints = viewHints;
  129.                 object = NULL;
  130.             }
  131.             
  132.         }
  133.         
  134.         if (object != NULL) 
  135.             Q3Object_Dispose(object);
  136.     }
  137.     
  138.     // If we found an error while reading, dispose of objects and exit with error.
  139.     if (Q3Error_Get(NULL) != kQ3ErrorNone) {
  140.     
  141.         if (model != NULL) {    
  142.             Q3Object_Dispose(model); 
  143.             model = NULL;
  144.             theDocument->documentGroup = NULL;
  145.         }
  146.     
  147.         if (viewHints != NULL) {    
  148.             Q3Object_Dispose(viewHints); 
  149.             viewHints = NULL;
  150.             theDocument->viewHints = NULL;
  151.         }
  152.         return (kQ3Failure);
  153.     }
  154.     
  155.     // Add lights to view if found.  Otherwise create default ones.
  156.     if (viewHints)
  157.         Q3ViewHints_GetLightGroup((TQ3ViewHintsObject)viewHints, &lightGroup);
  158.     if (lightGroup) {
  159.         Q3View_SetLightGroup(view, lightGroup);
  160.         Q3Object_Dispose(lightGroup);
  161.     } else {
  162.         MyNewLights(theDocument);
  163.     }
  164.         
  165.     // Add camera to view if found.  Otherwise create default one.
  166.     if (viewHints)
  167.         Q3ViewHints_GetCamera((TQ3ViewHintsObject)viewHints, &camera);
  168.     if (camera) {
  169.         Q3View_SetCamera(view, camera);
  170.         MyGetCameraData(theDocument, camera); // setup the camera data
  171.         Q3Object_Dispose(camera);
  172.     } else {
  173.         camera = MyNewCamera( theDocument->theWindow ) ;
  174.         Q3View_SetCamera(view, camera );
  175.         MyGetCameraData(theDocument, camera); // setup the camera data
  176.         Q3Object_Dispose(camera);
  177.     }
  178.     
  179.     // Add a couple other viewhints found to view (you may want to add the
  180.     // rest including all the drawcontext hints, etc.).
  181.     if (viewHints)
  182.         Q3ViewHints_GetAttributeSet((TQ3ViewHintsObject)viewHints, &attributeSet);
  183.     if (attributeSet) {
  184.         Q3View_SetDefaultAttributeSet(view, attributeSet);
  185.         Q3Object_Dispose(attributeSet);
  186.     }
  187.     if (viewHints)
  188.         Q3ViewHints_GetRenderer((TQ3ViewHintsObject)viewHints, &renderer);
  189.     if (renderer) {
  190.         Q3View_SetRenderer(view, renderer);
  191.         Q3Object_Dispose(renderer);
  192.     }
  193.     
  194.     // Initialize Delta factors based on model read.
  195.     MyInitDeltaFactors(theDocument);
  196.     
  197.     Q3File_Close(file);
  198.     
  199.     if (isEOF == kQ3False) {
  200.         if (model != NULL) {
  201.             Q3Object_Dispose(model);
  202.             model = theDocument->documentGroup = NULL;
  203.         }
  204.             
  205.         SetCursor(&qd.arrow);
  206.         return kQ3Failure;
  207.     }
  208.     
  209.     SetCursor(&qd.arrow);
  210.     return kQ3Success;
  211. }
  212.  
  213.  
  214.  
  215.  
  216.